package m202406.d29;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/29 17:31
 */

/**
 * n = 0;
 * p1=0
 * p2=length2-n-1
 * 3,2,1
 * 3,2,1,4,7
 * <p>
 * n=1
 * p1 = 0
 * p2 = length2-n-1
 * 3,2,1
 * 3,2,1,4,7
 * <p>
 * n=2
 * 3,2,1
 * 3,2,1,4,7
 * <p>
 * n=3
 * 3,2,1
 * 3,2,1,4,7
 * <p>
 * n=4
 * 3,2,1
 * 3,2,1,4,7
 * <p>
 * n=5
 * p1 = 1
 * p2 = 0;
 * 3,2,1
 * 3,2,1,4,7
 * <p>
 * n=6
 * p1 = 2
 * p2 =
 * 3,2,1
 * 3,2,1,4,7
 */
public class Q718 {

    public static void main(String[] args) {
        Q718 q718 = new Q718();
        q718.findLength(new int[]{3, 2, 1}, new int[]{5, 4, 3, 2, 1});
    }

    public int findLength(int[] nums1, int[] nums2) {
        int m = nums1.length;
        int n = nums2.length;
        int[][] dp = new int[m + 1][n + 1];
        int max = 0;
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                if (nums1[i] == nums2[j]) {
                    dp[i][j] = dp[i + 1][j + 1] + 1;
                } else {
                    dp[i][j] = 0;
                }

                max = Math.max(max, dp[i][j]);
            }
        }
        return max;
    }

    private int valid(int[] nums1, int[] nums2, int s1, int s2) {
        int e1 = s1;
        int e2 = s2;
        while (e1 < nums1.length && e2 < nums2.length) {
            if (nums1[e1] != nums2[e2]) {
                return e1 - s1;
            }
            e1++;
            e2++;
        }
        return Math.min(e1 - s1, e2 - s2);
    }
}
